home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: Pointer-to-Double as Function Arg
- Date: 16 Jan 1996 09:25:41 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4dgn2l$59p@crl.crl.com>
- References: <4dfccl$j5h@colossus.holonet.net>
- NNTP-Posting-Host: crl.com
-
- mitch@news.mdli.com (Mitch Miller) writes:
-
- >I'm having a problem with a function that takes a
- >couple of pointer-to-double arguments, initializes then
- >and assigns values to them in an array style:
-
- > int GetValue( long iDim, double * Values1, double *
- > Values2 )
- > {
- > int iter;
- > Values1 = (double *) malloc( iDim * sizeof( double ));
- > if (Values1 == NULL )
- > ....
- > Values2 = (double *) malloc( iDim * sizeof( double ));
- > if (Values2 == NULL )
- > ...
- > }
-
- This is another case of misunderstanding what call-by-value does. You are
- passing pointers to two doubles to GetValue, and then trying to modify
- those pointers (with the malloc() calls). You then go on to assign data
- to what's pointed to correctly, but when you return, your modified values
- are lost.
-
- What you need to do is declare GetValue as:
-
- int GetValue( int iDim, double **Values1, double **Values2)
- {
- ...
- *Values1 = malloc(...);
- *Values2 = malloc(...);
- ...
- }
- and then call it with:
- double *p1, *p2;
- int Size = whatever;
-
- if ( GetValue( Size, &p1, &p2))
- ...
-
- I hope that helps.
- Bob
-